package com.wl156.widget;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.ContextThemeWrapper;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.wl156.R;
import com.wl156.model.Database;

public class ProviceSelector extends LinearLayout {

	protected static final String TAG = null;
	private TextView mTitle;
	private Button mProvice;
	private Button mCity;
	private Button mRegion;

	private Database mDb;

	private ProviceAdapter mProviceAdapter;
	private ProviceAdapter mCityAdapter;
	private ProviceAdapter mRegionAdapter;

	private AlertDialog mProviceDialog;
	private AlertDialog mCityDialog;
	private AlertDialog mRegionDialog;

	private ListView mProviceListView;
	private ListView mCityListView;
	private ListView mRegionListView;

	private long mProviceId;
	private long mCityId;
	private long mRegionId;

	public ProviceSelector(final Context context, AttributeSet attrs) {
		super(context, attrs);

		mDb = Database.getInstance(context);

		mProviceListView = new ListView(context);
		mProviceListView.setSelected(true);
		mProviceListView.setCacheColorHint(Color.parseColor("#00000000"));
		mProviceListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (!mProvice.getText().toString()
						.equals(mProviceAdapter.getItemName(position))) {
					mCityId = -1;
					mCity.setText("");
					mRegion.setText("");
				}

				mProviceId = id;
				mProvice.setText(mProviceAdapter.getItemName(position));
				mProviceDialog.dismiss();
			}
		});
		mCityListView = new ListView(context);
		mCityListView.setCacheColorHint(Color.parseColor("#00000000"));
		mCityListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (!mCity.getText().toString()
						.equals(mCityAdapter.getItemName(position))) {
					mRegion.setText("");
				}

				mCityId = id;
				mCity.setText(mCityAdapter.getItemName(position));
				mCityDialog.dismiss();
			}
		});
		mRegionListView = new ListView(context);
		mRegionListView.setCacheColorHint(Color.parseColor("#00000000"));
		mRegionListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				mRegionId = id;
				mRegion.setText(mRegionAdapter.getItemName(position));
				mRegionDialog.dismiss();
			}
		});

		AlertDialog.Builder builder = new Builder(new ContextThemeWrapper(
				context, android.R.style.Theme_Dialog));
		builder.setView(mProviceListView);
		mProviceDialog = builder.create();
		builder.setView(mCityListView);
		mCityDialog = builder.create();
		builder.setView(mRegionListView);
		mRegionDialog = builder.create();

		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.widget_provice_selector, null);
		addView(view, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT));
		mTitle = (TextView) view.findViewById(R.id.title);

		mProvice = (Button) view.findViewById(R.id.provice);
		mProvice.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mProviceAdapter = new ProviceAdapter(context, mDb
						.selectProvice());
				mProviceListView.setAdapter(mProviceAdapter);
				mProviceDialog.show();
			}
		});
		mCity = (Button) view.findViewById(R.id.city);
		mCity.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mCityAdapter = new ProviceAdapter(context, mDb
						.selectCity(mProviceId));
				mCityListView.setAdapter(mCityAdapter);
				mCityDialog.show();
			}
		});
		mRegion = (Button) view.findViewById(R.id.region);
		mRegion.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Cursor cursor = mDb.selectZone(mCityId);
				if (cursor.getCount() <= 0) {
					Toast.makeText(getContext(), "无", Toast.LENGTH_SHORT)
							.show();
					return;
				}
				mRegionAdapter = new ProviceAdapter(context, cursor);
				mRegionListView.setAdapter(mRegionAdapter);
				mRegionDialog.show();
			}
		});
	}

	public void setTitle(String title) {
		mTitle.setText(title);
	}

	class ProviceAdapter extends CursorAdapter {
		public ProviceAdapter(Context context, Cursor c) {
			super(context, c);
		}

		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			View view = LayoutInflater.from(context).inflate(
					android.R.layout.simple_spinner_item, null);
			ListView.LayoutParams params = new ListView.LayoutParams(
					LayoutParams.FILL_PARENT, getResources()
							.getDimensionPixelSize(
									R.dimen.dialog_list_item_height));
			view.setLayoutParams(params);
			TextView tv = (TextView) view.findViewById(android.R.id.text1);
			tv.setBackgroundColor(Color.WHITE);
			tv.setTextColor(Color.BLACK);
			tv.setGravity(Gravity.CENTER_VERTICAL);
			tv.setText(cursor.getString(1));
			return view;
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			TextView tv = (TextView) view.findViewById(android.R.id.text1);
			tv.setText(cursor.getString(1));
		}

		@Override
		public Object getItem(int position) {
			return super.getItem(position);
		}

		@Override
		public long getItemId(int position) {
			return super.getItemId(position);
		}

		public String getItemName(int position) {
			return ((Cursor) getItem(position)).getString(1);
		}
	}

	public String getProvice() {
		return mProvice.getText().toString();
	}

	public String getCity() {
		return mCity.getText().toString();
	}

	public String getRegion() {
		return mRegion.getText().toString();
	}

	public void setProvice(String provice) {
		mProvice.setText(provice);
		mProviceId = mDb.getProviceIdByName(provice);
		// for (int i = 0; i < mProviceAdapter.getCount(); i++) {
		// Cursor c = (Cursor) mProviceAdapter.getItem(i);
		// if (c.getString(1).equals(provice)) {
		// mProviceId = c.getLong(0);
		// }
		// }
	}

	public void setCity(String city) {
		mCity.setText(city);
		mCityId = mDb.getCityIdByName(city);
		// for (int i = 0; i < mCityAdapter.getCount(); i++) {
		// Cursor c = (Cursor) mCityAdapter.getItem(i);
		// if (c.getString(1).equals(city)) {
		// mCityId = c.getLong(0);
		// }
		// }
	}

	public void setRegion(String region) {
		mRegion.setText(region);
	}

	public void setProviceRequried(boolean requried) {
		mProvice.setHint(requried ? "省(必填)" : "省");
	}

	public void setCityRequried(boolean requried) {
		mCity.setHint(requried ? "市(必填)" : "区");
	}

	public void setRegionRequried(boolean requried) {
		mRegion.setHint(requried ? "区(必填)" : "区");
	}

	public void hideRegion(boolean hide) {
		mRegion.setVisibility(hide ? GONE : VISIBLE);
	}
}
